package cn.magictodo.weblog.admin.event.subscriber.statistics;

import cn.magictodo.weblog.admin.event.DeleteArticleEvent;
import cn.magictodo.weblog.admin.service.AdminStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @author liu
 * @since 2023-12-11 20:51
 * 消费者
 */
@Component
@Slf4j
public class DeleteArticleStatisticsSubscriber implements ApplicationListener<DeleteArticleEvent> {
    @Autowired
    private AdminStatisticsService adminStatisticsService;
    @Override
    @Async("threadPoolTaskExecutor")
    public void onApplicationEvent(DeleteArticleEvent event) {
        try {
//            线程睡眠来使得统计任务执行在其他任务完成后执行
            Thread.sleep(5000);
            adminStatisticsService.statisticsCategoryArticleTotal();
            adminStatisticsService.statisticsTagArticleTotal();
            log.info("==> 删除文章，重新统计各分类标签下文章总数");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
